/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/TS32Fx_LL_Driver/src/ts32fx_ll_uart.c
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   This file contains all the UART LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup UART_LL_Driver UART LL Driver
  * @ingroup  uart_interface_gr
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

  
/** @defgroup UART_LL_Interrupt UART LL Interrupt Handle function
  * @ingroup  UART_LL_Driver
  * @brief    UART LL Interrupt Handle function
  * @{
  */



/**
  * @}
  */

/** @defgroup UART_LL_Inti_Cfg UART LL Initialization And Configuration
  * @ingroup  UART_LL_Driver
  * @brief    UART LL Initialization And Configuration
  * @{
  */

/** 
  * @brief  Link layer UART 232 initialization function
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  p_cfg  : UART Configuration struct
  * @retval None.
  */
void ll_uart_init(UART_TypeDef *p_uart, TYPE_LL_UART_INIT *p_init)
{
    TX_ASSERT((UART0 == p_uart) || (UART1 == p_uart));
    u32 uart_con = 0;
    ll_uart_baudrate_set(p_uart, p_init->baudrate);
    uart_con = p_uart->CON;
    uart_con = (uart_con & (~LL_UARTX_CON_UART_WORK_MODE_MASK)) | LL_UARTX_CON_UART_WORK_MODE(p_init->work_mode);
    if(LL_UART_WORD_LENGTH_9B == p_init->bit_width_sel)
    {
        uart_con = uart_con | LL_UARTX_CON_UART_9BIT_EN;
        uart_con = uart_con & (~LL_UARTX_CON_UART_PARITY_EN);
    }
    else
    {
        uart_con = uart_con & (~LL_UARTX_CON_UART_9BIT_EN);
        if(LL_UART_PARITY_NO == p_init->parity)
        {
            uart_con = uart_con & (~LL_UARTX_CON_UART_PARITY_EN);
        }
        else if(LL_UART_PARITY_ODD == p_init->parity)
        {
            uart_con = uart_con | LL_UARTX_CON_UART_PARITY_EN;
            uart_con = uart_con | LL_UARTX_CON_UART_ODD_EN;
        }
        else
        {
            uart_con = uart_con | LL_UARTX_CON_UART_PARITY_EN;
            uart_con = uart_con & (~LL_UARTX_CON_UART_ODD_EN);
        }
    }
    if(LL_UART_STOP_2B == p_init->stop_bit_sel)
    {
        uart_con = uart_con | LL_UARTX_CON_UART_STOP_BIT;
    }
    else
    {
        uart_con = uart_con & (~LL_UARTX_CON_UART_STOP_BIT);
    }
    p_uart->CON = uart_con;
}

/** 
  * @brief  Link layer UART detele initialization function
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @retval None.
  */
void ll_uart_deinit(UART_TypeDef *p_uart)
{
    /* The system disable the UART module, includes turning off the clock for the module. */
    TX_ASSERT((UART0 == p_uart) || (UART1 == p_uart));
}

/** 
  * @brief  Link layer UART interrupt initialization function
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  p_cfg  : UART Configuration struct
  * @retval None.
  */
void ll_uart_ie_config(UART_TypeDef *p_uart, TYPE_LL_UART_IE_CFG *p_cfg)
{
    u32 uart_ier = 0;
    uart_ier = p_uart->CON;
    if(p_cfg->uart_tx_ie)
    {
        uart_ier |= LL_UARTX_CON_UART_TX_IE;
    }
    else
    {
        uart_ier &= ~LL_UARTX_CON_UART_TX_IE;
    }
    if(p_cfg->uart_rx_ie)
    {
        uart_ier |= LL_UARTX_CON_UART_RX_IE;
    }
    else
    {
        uart_ier &= ~LL_UARTX_CON_UART_RX_IE;
    }
     p_uart->CON = uart_ier;
}

/** 
  * @brief  Link layer uart_rs485_config function
  * @param  p_uart: The structure pointer of the UART1 is selected.
  * @param  p_cfg  : UART1 rs485 Configuration struct
  * @retval None.
  */
void ll_uart_rs485_config(UART_TypeDef *p_uart,TYPE_LL_UART_RS485_CFG *p_cfg)
{
    TYPE_LL_GPIO_INIT gpio_struct;
    if(UART1 == p_uart)
    {   
        gpio_struct.gpio_pin   = LL_GPIO_PIN_3 | LL_GPIO_PIN_2;
        gpio_struct.gpio_mode  = LL_GPIO_MODE_AF;
        ll_gpio_init(GPIOC,&gpio_struct);
        /* pc2 func0   re   fpga:j19     */
        ll_gpio_pin_af_config(GPIOC, LL_GPIO_PIN_SOURCE_3,LL_GPIO_AF_0);
        /* pc3 func0   de   fpga:h15     */  
        ll_gpio_pin_af_config(GPIOC, LL_GPIO_PIN_SOURCE_2,LL_GPIO_AF_0); 
        ll_uart_re_pol_set(p_uart, p_cfg->re_pol);
        ll_uart_de_pol_set(p_uart, p_cfg->de_pol);
        ll_uart_rs485_det_de_dat_set(p_uart, p_cfg->de_dat);
        ll_uart_rs485_det_de_at_set(p_uart, p_cfg->de_at);
        ll_uart_rs485_tat_re2de_t_set(p_uart, p_cfg->re2de_t);
        ll_uart_rs485_tat_de2re_t_set(p_uart, p_cfg->de2re_t);
        ll_uart_rs485_mode_set(p_uart, p_cfg->rs485_mode);
        ll_uart_rs485_re_en_set(p_uart, p_cfg->re_en);
        ll_uart_rs485_de_en_set(p_uart, p_cfg->de_en);
        ll_uart_rs485_en_set(p_uart, p_cfg->rs485_en);
    }
}

/** 
  * @brief  Link layer uart_rs485_config function
  * @param  p_uart: The structure pointer of the UART1 is selected.
  * @param  p_cfg  : UART1 dma Configuration struct
  * @retval None.
  */
void ll_uart_dma_config(UART_TypeDef *p_uart,TYPE_LL_UART_DMA_CFG *p_cfg)
{
    if(UART1 == p_uart)
    {   
        p_uart->TSTADR = p_cfg->dma_tx_addr;
        p_uart->RSTADR = p_cfg->dma_rx_addr;
        p_uart->TDMALEN = p_cfg->dma_want_tx_len;
        p_uart->RDMALEN = p_cfg->dma_want_rx_len;
        if(p_cfg->dma_rx_perr_ie)
        {
            ll_uart1_dma_rx_perr_interrupt_enable();
        }
        else
        {
            ll_uart1_dma_rx_perr_interrupt_disable();
        }
         if(p_cfg->dma_tx_ie)
        {
            ll_uart1_dma_tx_interrupt_enable();
        }
        else
        {
            ll_uart1_dma_tx_interrupt_disable();
        }
        if(p_cfg->dma_rx_ie)
        {
            ll_uart1_dma_rx_interrupt_enable();
        }
        else
        {
            ll_uart1_dma_rx_interrupt_disable();
        }
        if(p_cfg->dma_tx_en)
        {
            ll_uart1_dma_tx_enable();
        }
        else
        {
            ll_uart1_dma_tx_disable();
        }
        if(p_cfg->dma_rx_en)
        {
            ll_uart1_dma_rx_enable();
        }
        else
        {
            ll_uart1_dma_rx_disable();
        }
    }
}

/**
  * @}
  */

/** @defgroup UART_LL_Data_Transfers UART LL Data transfers functions
  * @ingroup  UART_LL_Driver
  * @brief    UART LL Data transfers functions 
  * @{
  */

  /**
  * @brief  Link layer UART timeout interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_interrupt_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_TO_IE;
}

  /**
  * @brief  Link layer UART timeout interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_interrupt_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= (~LL_UARTX_CON_UART_TO_IE);
}

  /**
  * @brief  Link layer UART frame err interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_ferr_interrupt_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_FERR_IE;
}

  /**
  * @brief  Link layer UART frame err interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_ferr_interrupt_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= (~LL_UARTX_CON_UART_FERR_IE);
}

  /**
  * @brief  Link layer UART tx finish interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_interrupt_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_TX_IE;
}

  /**
  * @brief  Link layer UART tx finish interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_interrupt_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= (~LL_UARTX_CON_UART_TX_IE);
}

  /**
  * @brief  Link layer UART rx finish interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_interrupt_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_RX_IE;
}

  /**
  * @brief  Link layer UART rx finish interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_interrupt_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= (~LL_UARTX_CON_UART_RX_IE);
}

  /**
  * @brief  Link layer UART1 dma rx data parity err interrupt enable
  * @retval None
  */
void ll_uart1_dma_rx_perr_interrupt_enable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_PERR_IE_MASK)) | LL_UARTX_DMACON_RX_DMA_PERR_IE;
}

  /**
  * @brief  Link layer UART dma rx data parity err interrupt disable
  * @retval None
  */
void ll_uart1_dma_rx_perr_interrupt_disable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_PERR_IE_MASK));
}

  /**
  * @brief  Link layer UART1 dma rx finish interrupt enable
  * @retval None
  */
void ll_uart1_dma_rx_interrupt_enable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_IE_MASK)) | LL_UARTX_DMACON_RX_DMA_IE;
}

  /**
  * @brief  Link layer UART dma rx finish interrupt disable
  * @retval None
  */
void ll_uart1_dma_rx_interrupt_disable(void)
{
    UART1->DMACON = UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_IE_MASK);
}

  /**
  * @brief  Link layer UART1 dma tx finish interrupt enable
  * @retval None
  */
void ll_uart1_dma_tx_interrupt_enable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_TX_DMA_IE_MASK)) | LL_UARTX_DMACON_TX_DMA_IE;
}

  /**
  * @brief  Link layer UART dma tx finish interrupt disable
  * @retval None
  */
void ll_uart1_dma_tx_interrupt_disable(void)
{
    UART1->DMACON = UART1->DMACON & (~LL_UARTX_DMACON_TX_DMA_IE_MASK);
}

  /**
  * @brief  Link layer UART tmr pwm output enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tmr_pwm_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_TMR_PWM_EN;
}

  /**
  * @brief  Link layer UART tmr pwm output disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tmr_pwm_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_TMR_PWM_EN);
}

  /**
  * @brief  Link layer UART rx timeout enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_TO_EN;
}

  /**
  * @brief  Link layer UART rx timeout disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_TO_EN);
}

  /**
  * @brief  Link layer UART tx inv enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_inv_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_TX_INV_EN;
}

  /**
  * @brief  Link layer UART tx inv disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_inv_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_TX_INV_EN);
}

  /**
  * @brief  Link layer UART rx inv enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_inv_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_RX_INV_EN;
}

  /**
  * @brief  Link layer UART rx inv disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_inv_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_RX_INV_EN);
}

  /**
  * @brief  Link layer UART odd enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_odd_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_ODD_EN;
}

  /**
  * @brief  Link layer UART odd disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_odd_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_ODD_EN);
}

  /**
  * @brief  Link layer UART parity enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_parity_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_PARITY_EN;
}

  /**
  * @brief  Link layer UART parity disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_parity_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_PARITY_EN);
}

  /**
  * @brief  Link layer UART 9bit enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_9bit_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_9BIT_EN;
}

  /**
  * @brief  Link layer UART 9bit disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_9bit_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_9BIT_EN);
}

  /**
  * @brief  Link layer UART enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_enable(UART_TypeDef *p_uart)
{
    p_uart->CON |= LL_UARTX_CON_UART_EN;
}

  /**
  * @brief  Link layer UART disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_disable(UART_TypeDef *p_uart)
{
    p_uart->CON &= ~(LL_UARTX_CON_UART_EN);
}

  /**
  * @brief  Link layer UART1 rx dma enable
  * @retval None
  */
void ll_uart1_dma_rx_enable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_EN_MASK)) | LL_UARTX_DMACON_RX_DMA_EN;
}

  /**
  * @brief  Link layer UART1 rx dma disable
  * @retval None
  */
void ll_uart1_dma_rx_disable(void)
{
    UART1->DMACON = UART1->DMACON & (~LL_UARTX_DMACON_RX_DMA_EN_MASK);
}

  /**
  * @brief  Link layer UART1 tx dma enable
  * @retval None
  */
void ll_uart1_dma_tx_enable(void)
{
    UART1->DMACON = (UART1->DMACON & (~LL_UARTX_DMACON_TX_DMA_EN_MASK)) | LL_UARTX_DMACON_TX_DMA_EN;
}

  /**
  * @brief  Link layer UART1 tx dma disable
  * @retval None
  */
void ll_uart1_dma_tx_disable(void)
{
    UART1->DMACON = UART1->DMACON & (~LL_UARTX_DMACON_TX_DMA_EN_MASK);
}

  /**
  * @brief  Link layer UART1 rs485 re enable
  * @retval None
  */
void ll_uart1_rs485_re_enable(void)
{
    UART1->RS485_CON |= LL_UARTX_RS485_CON_RE_EN;
}

  /**
  * @brief  Link layer UART1 rs485 re disable
  * @retval None
  */
void ll_uart1_rs485_re_disable(void)
{
    UART1->RS485_CON &= (~LL_UARTX_RS485_CON_RE_EN);
}

  /**
  * @brief  Link layer UART1 rs485 de enable
  * @retval None
  */
void ll_uart1_rs485_de_enable(void)
{
    UART1->RS485_CON |= LL_UARTX_RS485_CON_DE_EN;
}

  /**
  * @brief  Link layer UART1 rs485 de disable
  * @retval None
  */
void ll_uart1_rs485_de_disable(void)
{
    UART1->RS485_CON &= (~LL_UARTX_RS485_CON_DE_EN);
}

  /**
  * @brief  Link layer UART1 rs485 enable
  * @retval None
  */
void ll_uart1_rs485_enable(void)
{
    UART1->RS485_CON |= LL_UARTX_RS485_CON_RS485_EN;
}

  /**
  * @brief  Link layer UART1 rs485 disable
  * @retval None
  */
void ll_uart1_rs485_disable(void)
{
    UART1->RS485_CON &= (~LL_UARTX_RS485_CON_RS485_EN);
}















/** 
  * @brief  uart baudrate set
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  baudrate: uart baudrate.
  * @retval result.
  */
void ll_uart_baudrate_set(UART_TypeDef *p_uart, uint32_t baudrate)
{
    p_uart->BAUD = SYS_CLK/baudrate - 1;
}

/** 
  * @brief  uart work mode get(duplex, single send, single receive, single auto)
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @retval work mode enum.
  */
TYPE_ENUM_LL_UART_WORK_MODE ll_uart_work_mode_get(UART_TypeDef *p_uart)
{
    
    return (TYPE_ENUM_LL_UART_WORK_MODE)(((p_uart->CON) >> 1) & 0x03);
}

/** 
  * @brief  uart work mode set(duplex, single send, single receive, single auto)
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  work_mode: work mode enum.
  * @retval result.
  */
void ll_uart_work_mode_set(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_WORK_MODE work_mode)
{
    p_uart->CON = (p_uart->CON & (~LL_UARTX_CON_UART_WORK_MODE_MASK)) | LL_UARTX_CON_UART_WORK_MODE(work_mode);
}

/** 
  * @brief  uart irq tx
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  tx_data: tx uart data.
  * @retval result.
  * @note   uart tx done pending need clear in irq interrupt,and tx next data in the uart irq interrupt.
  */
void ll_uart_irq_tx(UART_TypeDef *p_uart, u16 tx_data)
{
    p_uart->DATA = tx_data;
}

/** 
  * @brief  uart tx
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  tx_data: tx uart data.
  * @retval result.
  * @note   Uart tx done pending need clear in this function, because, not using irq.
  */
void ll_uart_tx(UART_TypeDef *p_uart,u16 tx_data)
{
    p_uart->DATA = tx_data;
    while(!LL_UART_TX_DONE_PENDING_GET(p_uart));
    LL_UART_TX_DONE_PENDING_CLR(p_uart);
}

/** 
  * @brief  uart rx
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  rx_data: uart rx data.
  * @retval true or false.
  * @note   uart rx buf not empty  need clear or not in this function, because, chip can do when rx buf is empty.
  */
bool ll_uart_rx(UART_TypeDef *p_uart, u16 *rx_data)
{
    while(LL_UART_RX_BUF_NOT_EMPTY_PENDING_GET(p_uart))
    {
        *rx_data = p_uart->DATA;
        /* p_uart->UARTSTA = UARTX_STA_RX_BUF_NOT_EMPTY; */
    return true;
    }
    return false;
}



/** 
  * @brief  uart tx inv en get
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_tx_inv_en_get(UART_TypeDef *p_uart)
{
    if((p_uart->CON) & LL_UARTX_CON_UART_TX_INV_EN)
    {
        return ENABLE;
    }
    else
    {
        return DISABLE;
    }
}

/** 
  * @brief  uart rx inv en get
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_rx_inv_en_get(UART_TypeDef *p_uart)
{
    if((p_uart->CON) & LL_UARTX_CON_UART_RX_INV_EN)
    {
        return ENABLE;
    }
    else
    {
        return DISABLE;
    }
}

/** 
  * @brief  uart 9bit en get
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_9bit_en_get(UART_TypeDef *p_uart)
{
    if((p_uart->CON) & LL_UARTX_CON_UART_9BIT_EN)
    {
        return ENABLE;
    }
    else
    {
        return DISABLE;
    }
}

/** 
  * @brief  uart update detect en get
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart0_updata_detect_en_get(UART_TypeDef *p_uart)
{
    if((UART0 == p_uart) && ((p_uart->STA) & LL_UARTX_STA_UPDATE_DETECT_EN))
    {
        return ENABLE;
    }
    else
    {
        return DISABLE;
    }
}

/* uart dma */


/** 
  * @brief  uart dma tx address set(9bit data address must 2bytes align, 8bit data address is arbitrary)
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  addr: dma address.  
  * @retval result.
  */
void ll_uart_dma_tx_addr_set(UART_TypeDef *p_uart, u32 addr)
{
    if(UART1 == p_uart)
    {
        p_uart->TSTADR = LL_UARTX_DMA_RX_TX_ADDRESS(addr);
    }
}

/** 
  * @brief  uart dma rx address set(9bit data address must 2bytes align, 8bit data address is arbitrary)
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  addr: dma address.  
  * @retval result.
  */
void ll_uart_dma_rx_addr_set(UART_TypeDef *p_uart, u32 addr)
{
    if(UART1 == p_uart)
    {
        p_uart->RSTADR = LL_UARTX_DMA_RX_TX_ADDRESS(addr);
    }
}

/** 
  * @brief  uart dma want tx len set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  len: want tx len.  
  * @retval result.
  */
void ll_uart_dma_want_tx_len_set(UART_TypeDef *p_uart, u16 len)
{
    if(UART1 == p_uart)
    {
        p_uart->TDMALEN = LL_UARTX_DMA_WANT_RX_TX_LEN(len);
    }
}

/** 
  * @brief  uart dma want rx len set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  len: want rx len.  
  * @retval result.
  */
void ll_uart_dma_want_rx_len_set(UART_TypeDef *p_uart, u16 len)
{
    if(UART1 == p_uart)
    {
        p_uart->RDMALEN = LL_UARTX_DMA_WANT_RX_TX_LEN(len);
    }
}

/** 
  * @brief  uart dma want tx len get
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected. 
  * @retval want tx len.
  */
u16 ll_uart_dma_want_tx_len_get(UART_TypeDef *p_uart)
{
    if(UART1 == p_uart)
    {
        return (p_uart->TDMALEN);
    }
    else
    {
        return 0;
    }
}

/** 
  * @brief  uart dma want rx len get
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected. 
  * @retval want rx len.
  */
u16 ll_uart_dma_want_rx_len_get(UART_TypeDef *p_uart)
{
    if(UART1 == p_uart)
    {
        return (p_uart->RDMALEN);
    }
    else
    {
        return 0;
    }
}

/** 
  * @brief  uart dma already tx len get
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected. 
  * @retval already tx len.
  */
u16 ll_uart_dma_tx_len_get(UART_TypeDef *p_uart)
{
    if(UART1 == p_uart)
    {
        return (p_uart->TDMACNT);
    }
    else
    {
        return 0;
    }
}

/** 
  * @brief  uart dma already rx len get
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected. 
  * @retval already rx len.
  */
u16 ll_uart_dma_rx_len_get(UART_TypeDef *p_uart)
{
    if(UART1 == p_uart)
    {
        return (p_uart->RDMACNT);
    }
    else
    {
        return 0;
    }
}

/* uart rs485 */
/** 
  * @brief  uart rs485 re en set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  enable: ENABLE or DISABLE.  
  * @retval result.
  */
void ll_uart_rs485_re_en_set(UART_TypeDef *p_uart, FunctionalState enable)
{
    if(UART1 == p_uart)
    {
        if(enable)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_RE_EN;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_RE_EN);
        }
    }
}

/** 
  * @brief  uart rs485 de en set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  enable: ENABLE or DISABLE.  
  * @retval result.
  */
void ll_uart_rs485_de_en_set(UART_TypeDef *p_uart, FunctionalState enable)
{
    if(UART1 == p_uart)
    {
        if(enable)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_DE_EN;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_DE_EN);
        }
    }
}

/** 
  * @brief  uart rs485 work mode get
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @retval TYPE_ENUM_LL_UART_RS485_MODE.
  */
TYPE_ENUM_LL_UART_RS485_MODE ll_uart_rs485_mode_get(UART_TypeDef *p_uart)
{
    if(UART1 == p_uart)
    {
        return (TYPE_ENUM_LL_UART_RS485_MODE)((((p_uart->RS485_CON)>>3) & 0x01));
    }
    else
    {
        return LL_UART_RS485_MODE_MANUAL;
    }
}

/** 
  * @brief  uart rs485 work mode set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  rs485_mode: TYPE_ENUM_LL_UART_RS485_MODE.
  * @retval result.
  */
void ll_uart_rs485_mode_set(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_RS485_MODE rs485_mode)
{
    if(UART1 == p_uart)
    {
        if(LL_UART_RS485_MODE_AUTO == rs485_mode)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_RS485_MODE;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_RS485_MODE);
        }
    }
}

/** 
  * @brief  uart rs485 re polarity set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  pol: TYPE_ENUM_LL_UART_RS485_RE_DE_POL.
  * @retval result.
  */
void ll_uart_re_pol_set(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_RS485_RE_DE_POL pol)
{
    if(UART1 == p_uart)
    {
        if(LL_UART_RS485_RE_DE_POL_L == pol)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_RE_POL;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_RE_POL);
        }
    }
}

/** 
  * @brief  uart rs485 de polarity set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  pol: TYPE_ENUM_LL_UART_RS485_RE_DE_POL.
  * @retval result.
  */
void ll_uart_de_pol_set(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_RS485_RE_DE_POL pol)
{
    if(UART1 == p_uart)
    {
        if(LL_UART_RS485_RE_DE_POL_L == pol)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_DE_POL;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_DE_POL);
        }
    }
}

/** 
  * @brief  uart rs485 en set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  enable: ENABLE or DISABLE.
  * @retval result.
  */
void ll_uart_rs485_en_set(UART_TypeDef *p_uart, FunctionalState enable)
{
    if(UART1 == p_uart)
    {
        if(enable)
        {
            p_uart->RS485_CON |= LL_UARTX_RS485_CON_RS485_EN;
        }
        else
        {
            p_uart->RS485_CON &= (~LL_UARTX_RS485_CON_RS485_EN);
        }
    }
}

/** 
  * @brief  uart rs485 det de at set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  interval: time interval.
  * @retval result.
  * @note   unit : uart clock, bit[0]~bit[8], time interval from de rising edge to send start bit
  */
void ll_uart_rs485_det_de_at_set(UART_TypeDef *p_uart, u16 interval)
{
    if(UART1 == p_uart)
    {
        p_uart->RS485_DET = (p_uart->RS485_DET & (~LL_UARTX_RS485_DET_DE_AT_MASK)) | LL_UARTX_RS485_DET_DE_AT(interval);
    }
}

/** 
  * @brief  uart rs485 det de dat set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  interval: time interval.
  * @retval result.
  * @note   unit : uart clock, bit[16]~bit[24], time interval from stop bit to de falling edge
  */
void ll_uart_rs485_det_de_dat_set(UART_TypeDef *p_uart,u16 interval)
{
    if(UART1 == p_uart)
    {
        p_uart->RS485_DET = (p_uart->RS485_DET & (~LL_UARTX_RS485_DET_DE_DAT_MASK)) | LL_UARTX_RS485_DET_DE_DAT(interval);
    }
}

/** 
  * @brief  uart rs485 tat de2re set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  interval: time interval.
  * @retval result.
  * @note   uart clock, bit[0]~bit[15], time interval from de falling edge to re rising edge
  */
void ll_uart_rs485_tat_de2re_t_set(UART_TypeDef *p_uart, u16 interval)
{
    if(UART1 == p_uart)
    {
        p_uart->RS485_TAT = (p_uart->RS485_TAT & (~LL_UARTX_RS485_TAT_DE2RE_T_MASK)) | LL_UARTX_RS485_TAT_DE2RE_T(interval);
    }
}

/** 
  * @brief  uart rs485 tat re2de set
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  interval: time interval.
  * @retval result.
  * @note   unit : uart clock, bit[16]~bit[31], time interval from re falling edge to de rising edge
  */
void ll_uart_rs485_tat_re2de_t_set(UART_TypeDef *p_uart, u16 interval)
{
    if(UART1 == p_uart)
    {
        p_uart->RS485_TAT = (p_uart->RS485_TAT & (~LL_UARTX_RS485_TAT_RE2DE_T_MASK)) | LL_UARTX_RS485_TAT_RE2DE_T(interval);
    }
}


/**
  * @}
  */

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
